/**
 * 
 */
package com.project.solomon.MBean;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.event.RowEditEvent;

import com.project.entity.Account;
import com.project.entity.PropForRent;
import com.project.entity.Property;

import com.project.solomon.SessionBean.PropertyBeanRemote;

/**
 * @author Solomon
 * 
 */
@ManagedBean(name = "mProperty")
@SessionScoped
public class MProperty {

	private Property prop;

	private List<Property> allProp;
	private List<Property> allPropFromAcc;

	private String currentPropertyID;
	private String propselectedtype = "p4r";
	private boolean imageUploaded;
	private boolean status;

	public MProperty() throws Exception {
		prop = new Property();
		prop.setPropForRent(new PropForRent());
	}

	public void setImageUploaded(boolean imageUploaded) {
		this.imageUploaded = imageUploaded;
	}

	public boolean isImageUploaded() {
		return imageUploaded;
	}

	public void setPropselectedtype(String propselectedtype) {
		this.propselectedtype = propselectedtype;
	}

	public String getPropselectedtype() {
		return propselectedtype;
	}

	public String getPropType(Property prop) {
		return prop.getPropID().substring(0, 1);
	}

	public void setProp(Property prop) {
		this.prop = prop;
	}

	public Property getProp() {
		return prop;
	}

	public void setAllProp(List<Property> allProp) {
		this.allProp = allProp;
	}

	public List<Property> getAllProp() throws Exception {
		if (allProp == null) {
			Context ctx = new InitialContext();
			PropertyBeanRemote propRe = (PropertyBeanRemote) ctx
					.lookup("MySweetHome/PropertyBean/remote");

			allProp = propRe.getAllProp();
			if (allProp.size() != 0) {
				return allProp;
			} else
				return null;
		} else {
			return this.allProp;
		}

	}

	public void setAllPropFromAcc(List<Property> allPropFromAcc) {
		this.allPropFromAcc = allPropFromAcc;
	}

	public List<Property> getAllPropFromAcc() throws Exception{
		HttpServletRequest req = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Object obj = req.getSession().getAttribute("mAccount");
		Account currentAccount = ((com.project.tructc.MBean.MAccount) obj).getAcc();

		if (allPropFromAcc == null) {
			Context ctx = new InitialContext();
			PropertyBeanRemote propRe = (PropertyBeanRemote) ctx
					.lookup("MySweetHome/PropertyBean/remote");

			allPropFromAcc = propRe.getAllPropFromAcc(currentAccount);
			if (allPropFromAcc.size() != 0) {
				return allPropFromAcc;
			} else
				return null;
		} else {
			return this.allPropFromAcc;
		}
	}

	public void setCurrentPropertyID(String currentPropertyID) {
		this.currentPropertyID = currentPropertyID;
	}

	public String getCurrentPropertyID() {
		return currentPropertyID;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean isStatus() {
		return status;
	}

	public void statusRowUpdate(RowEditEvent evt) throws Exception {

		Context ctx = new InitialContext();
		PropertyBeanRemote propRe = (PropertyBeanRemote) ctx
				.lookup("MySweetHome/PropertyBean/remote");

		Property newProp = (Property) evt.getObject();

		FacesContext context = FacesContext.getCurrentInstance();

		boolean check = propRe.updateProperty(newProp);

		if (check) {
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Successful",
					"Update Successful!"));
		} else {
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error",
					"Status Update failed"));
		}
	}

	public void updateProperty() throws Exception {

		InitialContext ctx = new InitialContext();
		PropertyBeanRemote propRe = (PropertyBeanRemote) ctx
				.lookup("MySweetHome/PropertyBean/remote");

		FacesContext context = FacesContext.getCurrentInstance();

		boolean check = propRe.updateProperty(getProp());

		if (check)
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Successful",
					"Update Sucessful!"));
		else
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error", "Update failed"));
	}

	public String getPath(String propID) throws Exception {
		Context ctx = new InitialContext();
		PropertyBeanRemote propRe = (PropertyBeanRemote) ctx
				.lookup("MySweetHome/PropertyBean/remote");
		String path = propRe.getRandomIMGPath(propID);
		return path;
	}

	public List<String> getAllPath(String propID) throws Exception {
		Context ctx = new InitialContext();
		PropertyBeanRemote propRe = (PropertyBeanRemote) ctx
				.lookup("MySweetHome/PropertyBean/remote");
		List<String> allpath = propRe.getAllIMGPath(propID);
		return allpath;
	}

	public String validStatus(boolean status) {
		if (status == true)
			return "Avaible";
		else
			return "Unavaible";
	}

}
